import pymysql
import json
class OperationMysql():
    def __init__(self):
        self.conn = pymysql.connect(
            host='localhost',
            port=3306,
            user='root',
            password='123456',
            db='mql',
            charset='utf8',
            # 下面这句可以拿到字段名称
            cursorclass=pymysql.cursors.DictCursor
        )
        self.cur = self.conn.cursor()

        #查询一条数据
    def search_one(self,sql):
        self.cur.execute(sql)
        # 获取其数据
        result=self.cur.fetchone()
        #Flase时为可以正常显示中文
        return json.dumps(result,ensure_ascii=False)

if __name__=='__main__':
    op_mysql=OperationMysql()
    print(op_mysql.search_one("select *from user where id=1"))
